using System.Collections;
using System.Collections.Generic;
using Microsoft.Unity.VisualStudio.Editor;
using UnityEngine;

public class Board : MonoBehaviour
{
    public GameObject cellPrefab;
    public Transform bg;

    public Dictionary<Vector2, Cell> BoardCells = new Dictionary<Vector2, Cell>();

    public void CreateBoard() //创建新的棋盘，并把子物体名字改了
    {
        for (int y = 0; y < GameManager.Instance.height; y++)
        {
            for (int x = 0; x < GameManager.Instance.weight; x++)
            {
                GameObject obj = Instantiate(cellPrefab, new Vector3(x, y, 0),
                    Quaternion.identity, transform);
                Cell cell = obj.GetComponent<Cell>();
                cell.name = x + "_" + y;
                BoardCells.Add(cell.transform.position, cell);
                cell._cellState = CellState.Hide;
            }
        }

        bg.transform.localScale = new Vector3(GameManager.Instance.weight,
            GameManager.Instance.height, 0);
        bg.transform.position = new Vector3(GameManager.Instance.weight * 0.5f - 0.5f,
            GameManager.Instance.height * 0.5f - 0.5f, 0);
    }

    public void ClearCell() //消除固定格子
    {
        for (int y = 0; y < GameManager.Instance.height; y++)
        {
            List<Vector2> FixedCellPos = new List<Vector2>();
            for (int x = 0; x < GameManager.Instance.weight; x++)
            {
                if (BoardCells[new Vector2(x, y)]._cellState == CellState.Fixed)
                    FixedCellPos.Add(new Vector2(x, y));
            }

            if (FixedCellPos.Count == GameManager.Instance.weight) //则说明有固定行，进行消除
            {
                foreach (Vector2 cellPo in FixedCellPos)
                {
                    BoardCells[cellPo]._cellState = CellState.Hide;
                }

                GameManager.Instance.score += 10;
                GameManager.Instance.RefreshUI();
            }
        }

        AutoFall(); //TODO 落下代码
    }

    void AutoFall() //判定，每一行是否要自动落下一格
    {
        for (int y = 0; y < GameManager.Instance.height; y++)
        {
            if (SpecifyRowIsFixed(y)) continue; //如果本行为空，就不检测本行是否要下落了
            if (SpecifyRowIsFixed(y - 1)) //如果本行的下面一行为空
            {
                List<Vector2> fallCellPos = new List<Vector2>();
                Debug.Log("需要下落的是 " + y + "行");
                for (int x = 0; x < GameManager.Instance.weight; x++)
                {
                    //查询格子的状态，如果是 Fixed 才下落，如果不是 Fixed 不理
                    if (BoardCells[new Vector2(x, y)]._cellState == CellState.Fixed)
                        fallCellPos.Add(new Vector2(x, y));
                }

                Debug.Log("需要下落格子的数量是 " + fallCellPos.Count + "个");
                foreach (Vector2 fallCellPo in fallCellPos)
                {
                    //先把下一行点亮，再把本行的关闭
                    BoardCells[fallCellPo + new Vector2(0, -1)]._cellState = CellState.Fixed;
                    BoardCells[fallCellPo]._cellState = CellState.Hide;
                }

                y = 0; //有多行需要下落的时候，要重复检测
            }
        }
    }

    bool SpecifyRowIsFixed(int y) //指定行是空吗，true 空，false 不为空
    {
        if (y < 0) return false; //如果是最下面一行，就返回 false 因为最下面那行不存在要下落的情况
        int fixedNumber = 0;
        for (int x = 0; x < GameManager.Instance.weight; x++)
        {
            if (!BoardCells.ContainsKey(new Vector2(x, y))) //bug 调试代码，测试报错的物体位置
                Debug.Log("报错的位置为 " + new Vector2(x, y));

            if (BoardCells[new Vector2(x, y)]._cellState == CellState.Hide)
                fixedNumber++;
        }

        return fixedNumber == GameManager.Instance.weight;
    }
}